<template>
  <div class="box">
    <el-card>
      <el-tabs>
        <!-- 放置页签 -->
        <el-tab-pane label="角色管理">
          <!-- 新增角色按钮 -->
          <el-row style="padding: 10px 0">
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="show = true"
            >新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table :data="list">
            <el-table-column type="index" label="序号" width="120" />
            <el-table-column prop="name" label="角色名称" width="240" />
            <el-table-column prop="description" label="描述" />
            <el-table-column label="操作">
              <template v-slot="{row}">
                <el-button size="small" type="success" @click="allocationFn(row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="redactFn(row)">编辑</el-button>
                <el-button size="small" type="danger" @click="delFn(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-row
            type="flex"
            justify="center"
            align="middle"
            style="height: 60px"
          >
            <!-- 分页组件 -->
            <el-pagination
              layout="prev, pager, next, total,jumper,sizes"
              :total="total"
              :page-sizes="[2, 5, 10]"
              :page-size="pages.pagesize"
              @current-change="currentChangeFn"
              @size-change="sizeChangeFn"
            />
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 新增弹框  before-close="hint"-->
    <el-dialog ref="dia" :title="roleForm.id && '修改窗口' || '新增窗口'" :visible.sync="show" :before-close="hint" @close="closeFn">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="200px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="show = false">取消</el-button>
          <!-- //新增数据按钮 -->
          <el-button size="small" type="primary" @click="addFn">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配弹窗 -->
    <AssignPerm ref="AssignPerm" :show-assign-dialog.sync="showAssignDialog" />
  </div>
</template>

<script>
// 引入获取权限接口
// 引入转换属性结构方法
// 引入接口方法
import { getRoleList, addRole, deleteRole, compileAPI, getRoleMessageAPI } from '@/api/setting'
import AssignPerm from './components/assign-perm.vue'
export default {
  name: 'Hhhhh',
  components: {
    AssignPerm
  },
  data() {
    return {
      // 控制分配角色弹窗显隐
      showAssignDialog: false,
      // 新增弹窗的显隐
      show: false,
      // 弹窗双向绑定的信息
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      list: [],
      total: 0, // 页码数
      pages: {
        page: 1,
        pagesize: 2
      }
    }
  },
  created() {
    this.getSettingList()
  },
  methods: {
    // 分配角色按钮
    allocationFn(id) {
      this.showAssignDialog = true
      this.$refs.AssignPerm.showOriginalFn(id)
    },
    // 获取角色列表
    async getSettingList() {
      const { total, rows } = await getRoleList(this.pages)
      this.list = rows
      this.total = total
      console.table(rows)
    },
    // 页码改变事件
    currentChangeFn(current) {
      this.pages.page = current
      this.getSettingList()
    },
    // 当前页显示数据条数改变事件
    sizeChangeFn(size) {
      this.pages.pagesize = size
      this.getSettingList()
    },
    // 新增/编辑信息事件
    addFn() {
      this.$refs.roleForm.validate(async(val) => {
        if (this.roleForm.id) {
          // 编辑信息
          //
          await compileAPI(this.roleForm)
          this.$message.success('编辑成功')
        } else {
          // 新增
          await addRole(this.roleForm)
          this.$message.success('新增成功')
        }
        this.closeFn()
      })
    },
    // 删除信息
    delFn(row) {
      this.$confirm(`确认要删除${row.name}吗`).then(async() => {
        await deleteRole(row.id)
        this.$message.success('删除成功')
        this.getSettingList()
      }).catch(() => {

      })
    },
    // 弹出编辑弹窗并回填数据
    async redactFn(row) {
      this.show = true
      const res = await getRoleMessageAPI(row.id)
      this.roleForm = res
    },
    // 关闭弹窗事件
    closeFn() {
      // 重置表单
      this.roleForm = {
        name: '',
        description: ''
      }

      this.$refs.roleForm.resetFields()
      this.getSettingList()
      this.show = false
    },
    hint(done) {
      Object.keys(this.roleForm).some((key) => {
        console.log(this.roleForm[key])
        // eslint-disable-next-line eqeqeq
        console.log(this.roleForm[key] == true)
        // eslint-disable-next-line eqeqeq
        return this.roleForm[key] == true
      })
      if (Object.keys(this.roleForm).some((key) => this.roleForm[key])) {
        this.$confirm('退出将清空编辑信息，确认退出吗').then(() => {
          done()
        }).catch(() => {

        })
      } else {
        done()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
